package com.bop.dehong.setting;

import java.util.ArrayList;
import java.util.TreeMap;

import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.base.dehong.http.RequestCallBack;
import com.base.dehong.util.ToastUtils;
import com.base.dehong.widget.SwipeListView;
import com.bop.dehong.R;
import com.bop.dehong.base.BaseActivity;
import com.bop.dehong.base.DeHongApp;
import com.bop.dehong.base.TopManager;
import com.bop.dehong.config.Config;
import com.bop.dehong.http.model.DeleCardModel;
import com.bop.dehong.http.model.MyBankCardModel;
import com.bop.dehong.http.model.MyBankCardRtnModel;
import com.bop.dehong.info.BankCardInfo;
import com.bop.dehong.info.EmptyInfo;
import com.bop.dehong.setting.BankCardAdapter.onRightItemClickListener;
import com.bop.dehong.info.entity.BankCardItemEntity;
import com.bop.dehong.login.LoginActivity;
import com.bop.dehong.util.WebUrlUtil;
import com.bop.dehong.webview.WebViewActivity;

/**
 * @author TangWenhan/YeFengyun
 *2015-11-20下午8:13:49
 */
public class BankCardActivity extends BaseActivity{
	private SwipeListView mSwipView;
	private BankCardAdapter mBankCardAdapter;
	private MyBankCardModel myBankCardModel;
	private MyBankCardRtnModel myBankCardRtnModel;
	private LinearLayout binding_bank_card_layout, bank_card_list;
	private Button button1;
	private TopManager mTitleManager;
	private TextView mNoticeTv;
	
	private DeleCardModel mDeleteCardModel;
	private String uid;
	
	@Override
	protected void initTop() {
		View mView = getView(R.id.top);
		mTitleManager = new TopManager(mView);
		mTitleManager.setTitle("银行卡");
		mTitleManager.setLeftImageVisibility(View.VISIBLE);
		mTitleManager.setRightText("添加");
		mTitleManager.setRightTextOnClick(this);
	}

	@Override
	public void onTitleLeftClick(View v) {
		setResult(RESULT_OK);
		super.onTitleLeftClick(v);
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                 && event.getRepeatCount() == 0) {
            setResult(RESULT_OK);
            finishCurrentActivity();
        	return true;
         }
         return super.onKeyDown(keyCode, event);
     }
	
	@Override
	protected void initUI() {
		mSwipView = getView(R.id.swipeListView1);
		if (mBankCardAdapter == null) {
			mBankCardAdapter = new BankCardAdapter(this, mSwipView.getRightViewWidth());
			mBankCardAdapter.setOnRightItemClickListener(rightItemClickListener);
		}
		mSwipView.setAdapter(mBankCardAdapter);
		bank_card_list = getView(R.id.bank_card_list);
		binding_bank_card_layout = getView(R.id.binding_bank_card_layout);
		button1 = getView(R.id.button1);
		
		mNoticeTv = getView(R.id.textView2);
		registerOnClickListener(button1,mNoticeTv);
	}

	@Override
	protected void initData() {
		uid = DeHongApp.mLoginEntity.uid;
		if (TextUtils.isEmpty(uid))
		{
			ToastUtils.show(this, "你还未登录，请先登录！");
			Intent intent = new Intent(this, LoginActivity.class);
			intent.putExtra(Config.GO_BACK, true);
			startActivityForResult(intent, 99);
			return;
		}
		showProgressDialog();
		if (myBankCardModel == null) {
			myBankCardModel = new MyBankCardModel(this, callback);
		}
		myBankCardModel.doNet(uid);
	}
	
	RequestCallBack<ArrayList<BankCardItemEntity>> callback = new RequestCallBack<ArrayList<BankCardItemEntity>>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}
		
		public void onSuccess(ArrayList<BankCardItemEntity> info) {
			if (info != null && info.size() > 0) {
				mBankCardAdapter.clear();
				binding_bank_card_layout.setVisibility(View.GONE);
				bank_card_list.setVisibility(View.VISIBLE);
				mBankCardAdapter.appendToList(info);
				mTitleManager.setRightTextVisibility(View.VISIBLE);
			}else {
				mBankCardAdapter.clear();
				mBankCardAdapter.notifyDataSetChanged();
				binding_bank_card_layout.setVisibility(View.VISIBLE);
				bank_card_list.setVisibility(View.GONE);
				mTitleManager.setRightTextVisibility(View.INVISIBLE);
			}
			super.onSuccess(info);
		};
		
	};
	
	protected void initData1() {
		showProgressDialog();
		if (myBankCardRtnModel == null) {
			myBankCardRtnModel = new MyBankCardRtnModel(this, callback1);
		}
		myBankCardRtnModel.doNet(DeHongApp.mLoginEntity.getUid());
	}
	
	RequestCallBack<BankCardInfo> callback1 = new RequestCallBack<BankCardInfo>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}
		
		public void onSuccess(BankCardInfo info) {
			if (info != null) {
				String toastString = "";
				if (info.succ == 0) {
					toastString = "绑卡中！";
				}else {
					toastString = "绑卡成功！";
				}
				ToastUtils.show(BankCardActivity.this, toastString);
				if (info.lists != null && info.lists.size() > 0) {
					mBankCardAdapter.clear();
					binding_bank_card_layout.setVisibility(View.GONE);
					bank_card_list.setVisibility(View.VISIBLE);
					mBankCardAdapter.appendToList(info.lists);
					mTitleManager.setRightTextVisibility(View.VISIBLE);
				}else {
					mBankCardAdapter.clear();
					mBankCardAdapter.notifyDataSetChanged();
					binding_bank_card_layout.setVisibility(View.VISIBLE);
					bank_card_list.setVisibility(View.GONE);
					mTitleManager.setRightTextVisibility(View.INVISIBLE);
				}
			}
			super.onSuccess(info);
		};
		
	};
	
	
	onRightItemClickListener rightItemClickListener = new onRightItemClickListener(){

		@Override
		public void onRightItemClick(View v, int position) {
			deletePosition = position;
			deleteCard(mBankCardAdapter.get(deletePosition));
		}

	};
	
	int deletePosition = -1;
	private void deleteCard(BankCardItemEntity info){
		if ("1".equals(info.getIsdefaultme())) {
			ToastUtils.show(this, "默认取现卡不能删除");
			return;
		}
		String cNo = info.getBank_account();
		if (cNo.length() < 4) {
			return;
		}
		cNo = cNo.substring(cNo.length()-4, cNo.length());
		showProgressDialog();
		if (mDeleteCardModel == null) {
			mDeleteCardModel = new DeleCardModel(this, deleteCallback);
		}
		mDeleteCardModel.doNet(uid, cNo);
	}
	
	RequestCallBack<EmptyInfo> deleteCallback = new RequestCallBack<EmptyInfo>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}

		@Override
		public void onSuccess(EmptyInfo info) {
			super.onSuccess(info);
			mSwipView.hiddenCurrentView();
			mBankCardAdapter.remove(deletePosition);
			mBankCardAdapter.notifyDataSetChanged();
		}
		
	};

	
	private String getUrl(){
		TreeMap<String, String> postValues = new TreeMap<String, String>();
		postValues.put("a", "bankcard");
		postValues.put("m", "User");
		postValues.put("uid", DeHongApp.mLoginEntity.uid);
		return WebUrlUtil.getWebUrl(this, Config.getIP() + Config.bandBankCard, postValues);
	}
	
	@Override
	protected int getContentViewId() {
		TAG = getClass().getName();
		return R.layout.act_bank_card;
	}

	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode !=  RESULT_OK) {
			return ;
		}
		if (requestCode == 99)
		{
			initData();
		}else
		if (requestCode == 100) {
//			ToastUtils.show(this, "绑定成功！");
//			initData();
			initData1();
		}else
		if (requestCode == 101) {
//			ToastUtils.show(this, "添加成功，暂在处理中！");
//			initData();
			initData1();
		}
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId()) {
		case R.id.button1:
			Intent intent = new Intent(this,WebViewActivity.class);
			intent.putExtra("title", "绑定银行卡");
			intent.putExtra("url", getUrl());
			intent.putExtra(Config.GO_BACK, true);
			startActivityForResult(intent, 100);
			break;
		case R.id.top_bar_right_txt:
			Intent intent1 = new Intent(this,WebViewActivity.class);
			intent1.putExtra("title", "添加银行卡");
			intent1.putExtra("url", getUrl());
			intent1.putExtra(Config.GO_BACK, true);
			startActivityForResult(intent1, 101);
			break;
		case R.id.textView2:
			Intent intent2 = new Intent(this,WebViewActivity.class);
			intent2.putExtra("title", "绑定银行卡须知");
			intent2.putExtra("url", Config.getIP() + Config.bindcard);
			intent2.putExtra(Config.GO_BACK, true);
			startActivity(intent2);
			break;
		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		// TODO Auto-generated method stub
		
	}
	
}

